package com.mj.activity;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TabHost.OnTabChangeListener;

import com.mj.R;
import com.mj.base.BaseActivity;
import com.mj.bean.Check;
import com.mj.fragment.Fragment_juyou;
import com.mj.fragment.Fragment_kaiju;
import com.mj.http.Json;
import com.mj.http.Url;
import com.mj.util.DensityUtil;
import com.mj.util.Info;

public class MainActivity extends BaseActivity {
	private FragmentTabHost mTabHost;
	private Button button1, button2, button3, button5;
	private RelativeLayout button4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		menu.getItem(0).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_1:
			getData_1();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void getData_1() {
		baseCallBack callBack = new baseCallBack() {
			@Override
			public void onSuccess(JSONObject object) {
				super.onSuccess(object);
				checkUpdate(object);
			}
		};
		String url = Url.check + "?vcode=" + Info.getVersionCode(this);
		getJson(url, callBack, false);
	}

	/***
	 * 检查更新信息
	 */
	private void checkUpdate(JSONObject object) {
		Check check = Json.getCheck(object);
		// 0表示没有更新,1表示建议更新，2表示强制更新
		switch (check.getHasnew()) {
		case 0:
			break;
		case 1:
			alert(check.getUrl());
			break;
		case 2:
			download(check.getUrl());
			break;
		}
	}

	/***
	 * 建议更新
	 */
	private void alert(final String url) {
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("提示").setMessage("检测到新版本，是否更新？");
		builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				download(url);
			}
		});
		builder.setNegativeButton("否", null);
		builder.show();
	}

	/***
	 * 调用系统DownloadManager 下载文件
	 */
	private void download(String url) {
		DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
		Uri uri = Uri.parse(url);
		Request request = new Request(uri);
		// 设置允许使用的网络类型，这里是移动网络和wifi都可以
		request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
		// 禁止发出通知，既后台下载，如果要使用这一句必须声明一个权限：android.permission.DOWNLOAD_WITHOUT_NOTIFICATION
		// request.setShowRunningNotification(false);
		// 显示下载界面
		request.setVisibleInDownloadsUi(true);
		// 开始下载同时得到该下载文件对应的下载ID并保存
		long id = downloadManager.enqueue(request);
		// 保存下载ID
		SharedPreferences preferences = getSharedPreferences("download", MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putLong("id", id);
		editor.commit();
	}

	private void init() {
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
		createBtns();
		mTabHost.addTab(mTabHost.newTabSpec("1").setIndicator(button1), Fragment_juyou.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("2").setIndicator(button2), Fragment_juyou.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("3").setIndicator(button3), Fragment_kaiju.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("4").setIndicator(button4), Fragment_juyou.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("5").setIndicator(button5), Fragment_juyou.class, null);
		editBtns();
		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String arg0) {
				Log.i("arg0", arg0);
			}
		});
	}

	/***
	 * 创建Tab的5个按钮
	 */
	private void createBtns() {
		button1 = new Button(this);
		button2 = new Button(this);
		button3 = new Button(this);
		button4 = (RelativeLayout) getLayoutInflater().inflate(R.layout.btn4, null);
		button5 = new Button(this);
	}

	/***
	 * 编辑Tab的5个按钮
	 */
	private void editBtns() {
		// 按钮原始宽高
		double width = 144;
		double height = 120;
		// 缩放比例
		double ratio = DensityUtil.getRatio(this);
		// 真实宽高
		int realWidth = (int) (width * ratio);
		int realHeight = (int) (height * ratio);
		aq.id(button1).background(R.drawable.btn1).width(realWidth, false).height(realHeight, false);
		aq.id(button2).background(R.drawable.btn2).width(realWidth, false).height(realHeight, false);
		aq.id(button3).background(R.drawable.btn3).width(realWidth, false).height(realHeight, false);
		aq.id(button4).background(R.drawable.btn4).width(realWidth, false).height(realHeight, false);
		aq.id(button5).background(R.drawable.btn5).width(realWidth, false).height(realHeight, false);
	}

}
